<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="theme-color" content="#222">

<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/6f697dad.js","daovoice")
  daovoice('init', {
      app_id: "6f697dad"
    });
  daovoice('update');
  </script>















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="带你学会Java基础课程,">





  <link rel="alternate" href="/atom.xml" title="达叔小生" type="application/atom+xml">






<meta name="description" content="用一切抱怨的时间去努力">
<meta name="keywords" content="带你学会Java基础课程">
<meta property="og:type" content="article">
<meta property="og:title" content="第77节:Java中的事务和数据库连接池和DBUtils">
<meta property="og:url" content="https://huangguangda.github.io/2019/01/27/1/index.html">
<meta property="og:site_name" content="达叔小生">
<meta property="og:description" content="用一切抱怨的时间去努力">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://huangguangda.github.io/images/40.jpg">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-de1eea934cb6fb28.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-b6bfcd0a9cd91b2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-1fb13fda59aec338.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-957f27422de5d893.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-5c0db96f0c601c3c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-2c89852209c47240.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-73c606289c50f5bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-82ccbc226de9aca1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-91cc7dc17a30b137.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f0484045a508ef73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f4b088361405d51c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-ba0363848fe54936.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-340d7d7c9fa17391.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d9106d65bda58b29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-5ddc6d9143e31aff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-e64e5a5deeb0b1c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f8bbe6bf9b1edaeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-c450391d3938cb67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-cdb604cd503e7150.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f7bf143f903f5ea4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-9b76968719b44c3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-88e62f1497ebc2e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-71768cebcf17d9c3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-36554b379444364c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-2a54bec6bdd2dc42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d0f4b3b9da1054f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-a4e5431c4351fb69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-1d7f007806a44df5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-748f14ac618bd972.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-e9a7fb886f61a2b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-abc1c8eabc71877b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-ab4e57154f848904.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-c7b28ae1809f056d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-cb00c0352e7e6afa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-ef698665a32c12f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-e0f2d760fae4d676.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-a536c4ba1f1d6304.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-9fef7bc147a7897b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-9c3847fe44648faf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-567b038b4c0f658c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-1f69b3e6719fe3ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-5439323e10ed52ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-4e07621f4afff961.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-28e95bc6dd2e238d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f849cba99e5c3b8e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-3dd1e1ddb5f434d6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-4535bfedba1a991d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-7d92e61a7b2a6340.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-b19bc37d5e4ec399.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-ab552bda614279ac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-1afb55e7867fc95e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-21b7e26bb44e8949.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-c7889696705c734f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d5023746f361db32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-8078c5a665e3ccce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-5d7951abe03baa0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-36d98c1365978dae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-4613be7b7ae6b406.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-4f0bb33cbb0d382b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d7050d50a84bb14f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f05618d59e909ffc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-be73f8e8b3fdc4f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-46c9098f2b9ee519.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-c8fa75e19f31660a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-7528888cd0b13a3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-b3e7cdea9f4befed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-716550bdcbdd7c09.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-5d4dfbddb425954e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-38c6f62adadcc132.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-eaeb4582c561c67b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-3156e9e65b334c6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-01-27T08:19:54.693Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第77节:Java中的事务和数据库连接池和DBUtils">
<meta name="twitter:description" content="用一切抱怨的时间去努力">
<meta name="twitter:image" content="https://huangguangda.github.io/images/40.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangguangda.github.io/2019/01/27/1/">




<!-- 网页加载条 -->
<script src="https://neveryu.github.io/js/src/pace.min.js"></script>
  <title>第77节:Java中的事务和数据库连接池和DBUtils | 达叔小生</title>
  









  <!-- 音频播放 -->
<link rel="stylesheet" href="/dist/APlayer.min.css">
<div id="aplayer"></div>
<script type="text/javascript" src="/dist/APlayer.min.js"></script>
<script type="text/javascript" src="/dist/music.js"></script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <a href="https://github.com/huangguangda" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">达叔小生</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">爱好分享的码农</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-首页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-关于">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-标签">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-分类">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-归档">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-书籍">
          <a href="/books" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br>
            
            书籍
          </a>
        </li>
      
        
        <li class="menu-item menu-item-电影">
          <a href="/movies" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-chrome"></i> <br>
            
            电影
          </a>
        </li>
      
        
        <li class="menu-item menu-item-站点">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点
          </a>
        </li>
      
        
        <li class="menu-item menu-item-公益">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>
            
            公益
          </a>
        </li>
      
        
        <li class="menu-item menu-item-课本">
          <a href="/book/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-apple"></i> <br>
            
            课本
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2019/01/27/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">第77节:Java中的事务和数据库连接池和DBUtils</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-27T16:17:07+08:00">
                2019-01-27
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/带你学会Java基础课程/" itemprop="url" rel="index">
                    <span itemprop="name">带你学会Java基础课程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  9,745
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  49
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote class="blockquote-center">用一切抱怨的时间去努力</blockquote>

<p><img src="/images/40.jpg" style="width: 100%;height: 100%"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-de1eea934cb6fb28.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="标题图"><br><a id="more"></a></p>
<blockquote>
<p>第77节:Java中的事务和数据库连接池和DBUtiles</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>看哭你,字数:8803,承蒙关照,谢谢朋友点赞!</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-b6bfcd0a9cd91b2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="字数8803"></p>
<h2 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h2><p><code>Transaction</code>事务,什么是事务,事务是包含一组操作,这组操作里面包含许多个单一的逻辑,只要有一个逻辑没有执行成功就算失败,导致回滚就是指所有的数据都会回到最初的状态.</p>
<p>有事务,是为了保证逻辑一定要成功,如银行转账.</p>
<h2 id="回顾一下"><a href="#回顾一下" class="headerlink" title="回顾一下"></a>回顾一下</h2><p>什么是<code>jsp</code>,<code>jsp</code>的三大指令.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">page: 定义当前页面的信息</span><br><span class="line">include: 包含其他页面</span><br><span class="line">taglib: 引入标签库</span><br></pre></td></tr></table></figure>
<p>三大动作标签:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:forward page=&quot;&quot;&gt;:跳转</span><br><span class="line">&lt;jsp:param name=&quot;&quot; value=&quot;&quot;&gt;:跳转到具体页面或是某个页面被包含的时候,可以指定参数</span><br><span class="line">&lt;jsp:include page=&quot;&quot;&gt;:包含</span><br></pre></td></tr></table></figure>
<p>九大内置对象:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// 四个作用域</span><br><span class="line">pageContext 当前页</span><br><span class="line">request 请求 一次请求 响应后就不行了</span><br><span class="line">session 会话 一次会话</span><br><span class="line">application 都可以 整个项目,直到关闭服务器</span><br><span class="line"></span><br><span class="line">// 5</span><br><span class="line">response</span><br><span class="line">out</span><br><span class="line">exception</span><br><span class="line">config -&gt; ServletConfig</span><br><span class="line">page -&gt; this 翻译成java文件的类对象</span><br></pre></td></tr></table></figure>
<h2 id="EL"><a href="#EL" class="headerlink" title="EL"></a><code>EL</code></h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$&#123; 表达式 &#125;;</span><br></pre></td></tr></table></figure>
<p>取值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;% User user = new User(); %&gt;</span><br><span class="line">$&#123;user.ame&#125;</span><br><span class="line">从四个作用域里面找 </span><br><span class="line">pageContext -&gt; request -&gt; session -&gt; application</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;%</span><br><span class="line"> User user = new User();</span><br><span class="line"> pageContext.setAttribute(&quot;user&quot;,user);</span><br><span class="line">%&gt;</span><br><span class="line">$&#123;sessionScope.user.name&#125;强制用session找</span><br></pre></td></tr></table></figure>
<p><code>EL</code>的11个隐式对象:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">pageContext</span><br><span class="line">// 作用域</span><br><span class="line">pageScope</span><br><span class="line">requestScope</span><br><span class="line">sessionScope</span><br><span class="line">applicationScope</span><br><span class="line"></span><br><span class="line">头相关</span><br><span class="line">header</span><br><span class="line">headerValues</span><br><span class="line">参数</span><br><span class="line">param</span><br><span class="line">paramValues</span><br><span class="line">cookie</span><br><span class="line">initparam</span><br></pre></td></tr></table></figure>
<h2 id="JSTL"><a href="#JSTL" class="headerlink" title="JSTL"></a><code>JSTL</code></h2><p>导包哦,引入标签库是1.1的:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;c:set&gt;: 存储到作用域</span><br><span class="line">&lt;c:set var=&quot;&quot; value=&quot;&quot; scope=&quot;&quot;&gt;</span><br><span class="line">&lt;c: if&gt;: 用来判断</span><br><span class="line">&lt;c:forEach items=&quot;&quot; var=&quot;&quot;&gt;:用于遍历</span><br></pre></td></tr></table></figure>
<h2 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-1fb13fda59aec338.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="添加两条数据"></p>
<p>数据库命令行:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-957f27422de5d893.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-5c0db96f0c601c3c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p>关闭自动提交.</p>
<p>关闭了提交,再减100:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-2c89852209c47240.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p>会到数据库查看:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-73c606289c50f5bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="刷新了"></p>
<p>值没改变!</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-82ccbc226de9aca1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="输入提交后"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-91cc7dc17a30b137.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="刷新改变"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f0484045a508ef73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="开启事务"></p>
<p>添加改变代码:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f4b088361405d51c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="先看表"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line"> </span><br><span class="line">public class TestDemo &#123; </span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testTransaction() &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		ResultSet rs = null;</span><br><span class="line">		try &#123;</span><br><span class="line">		    conn = JDBCUtil.getConn();</span><br><span class="line">		    //查询数据</span><br><span class="line">			/*String sql = &quot;select * from account&quot;;</span><br><span class="line">		    ps = conn.prepareStatement(sql);*/</span><br><span class="line">			</span><br><span class="line">		    String sql = &quot;update account set money = money - ? where id = ?&quot;;</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">		    </span><br><span class="line">		    // 扣钱</span><br><span class="line">			ps.setInt(1, 100);</span><br><span class="line">			ps.setInt(2, 1);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			/*ps.setInt(1, -100);</span><br><span class="line">			ps.setInt(2, 1);</span><br><span class="line">			ps.executeUpdate();*/</span><br><span class="line">			</span><br><span class="line">			// 查询数据 </span><br><span class="line">			/*rs = ps.executeQuery();</span><br><span class="line">			while(rs.next()) &#123;</span><br><span class="line">				System.out.println(rs.getString(&quot;name&quot;)+&quot;==&quot;+rs.getInt(&quot;money&quot;));</span><br><span class="line">			&#125;*/</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps, rs);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DriverManager;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">public class JDBCUtil &#123;</span><br><span class="line">	</span><br><span class="line">	static String driverClass = null;</span><br><span class="line">	static String url = null;</span><br><span class="line">	static String name = null;</span><br><span class="line">	static String password= null;</span><br><span class="line">	</span><br><span class="line">	static&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			//1. 创建一个属性配置对象</span><br><span class="line">			Properties properties = new Properties();</span><br><span class="line">			InputStream is = new FileInputStream(&quot;jdbc.properties&quot;);</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			//使用类加载器，去读取src底下的资源文件。 后面在servlet</span><br><span class="line">//			InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream(&quot;jdbc.properties&quot;);</span><br><span class="line">			//导入输入流。</span><br><span class="line">			properties.load(is);</span><br><span class="line">			</span><br><span class="line">			//读取属性</span><br><span class="line">			driverClass = properties.getProperty(&quot;driverClass&quot;);</span><br><span class="line">			url = properties.getProperty(&quot;url&quot;);</span><br><span class="line">			name = properties.getProperty(&quot;name&quot;);</span><br><span class="line">			password = properties.getProperty(&quot;password&quot;);</span><br><span class="line">			</span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 获取连接对象</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static Connection getConn()&#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		try &#123;</span><br><span class="line">			Class.forName(driverClass);</span><br><span class="line">			//静态代码块 ---&gt; 类加载了，就执行。 java.sql.DriverManager.registerDriver(new Driver());</span><br><span class="line">			//DriverManager.registerDriver(new com.mysql.jdbc.Driver());</span><br><span class="line">			//DriverManager.getConnection(&quot;jdbc:mysql://localhost/test?user=monty&amp;password=greatsqldb&quot;);</span><br><span class="line">			//2. 建立连接 参数一： 协议 + 访问的数据库 ， 参数二： 用户名 ， 参数三： 密码。</span><br><span class="line">			conn = DriverManager.getConnection(url, name, password);</span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		return conn;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 释放资源</span><br><span class="line">	 * @param conn</span><br><span class="line">	 * @param st</span><br><span class="line">	 * @param rs</span><br><span class="line">	 */</span><br><span class="line">	public static void release(Connection conn , Statement st , ResultSet rs)&#123;</span><br><span class="line">		closeRs(rs);</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line">	public static void release(Connection conn , Statement st)&#123;</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	private static void closeRs(ResultSet rs)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(rs != null)&#123;</span><br><span class="line">				rs.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			rs = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeSt(Statement st)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(st != null)&#123;</span><br><span class="line">				st.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			st = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeConn(Connection conn)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(conn != null)&#123;</span><br><span class="line">				conn.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			conn = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-ba0363848fe54936.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="刷新后"></p>
<p>事务只针对连接.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line"> </span><br><span class="line">public class TestDemo &#123; </span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void test02()&#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		ResultSet rs = null;</span><br><span class="line">		try &#123;</span><br><span class="line">			conn = JDBCUtil.getConn();</span><br><span class="line">			</span><br><span class="line">			String sql = &quot;update account set money = money - ? where id = ?&quot;;</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			//扣钱， 扣ID为1 的100块钱</span><br><span class="line">			ps.setInt(1, 100);</span><br><span class="line">			ps.setInt(2, 1);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			//int a = 10 /0 ;</span><br><span class="line">			String s = null;</span><br><span class="line">			s.length();</span><br><span class="line">			</span><br><span class="line">			//加钱， 给ID为2 加100块钱</span><br><span class="line">			ps.setInt(1, -100);</span><br><span class="line">			ps.setInt(2, 2);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">			</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps, rs);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testTransaction() &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		ResultSet rs = null;</span><br><span class="line">		try &#123;</span><br><span class="line">		    conn = JDBCUtil.getConn();</span><br><span class="line">		    </span><br><span class="line">		    // 连接</span><br><span class="line">		    conn.setAutoCommit(false);</span><br><span class="line">		    </span><br><span class="line">		    //查询数据</span><br><span class="line">			/*String sql = &quot;select * from account&quot;;</span><br><span class="line">		    ps = conn.prepareStatement(sql);*/</span><br><span class="line">			</span><br><span class="line">		    String sql = &quot;update account set money = money - ? where id = ?&quot;;</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">		    </span><br><span class="line">		    // 扣钱</span><br><span class="line">			ps.setInt(1, 100);</span><br><span class="line">			ps.setInt(2, 1);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			int a = 10 /0 ;</span><br><span class="line">			</span><br><span class="line">			ps.setInt(1, -100);</span><br><span class="line">			ps.setInt(2, 2);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">			// 提交事务</span><br><span class="line">			conn.commit();</span><br><span class="line">			</span><br><span class="line">			// 查询数据 </span><br><span class="line">			/*rs = ps.executeQuery();</span><br><span class="line">			while(rs.next()) &#123;</span><br><span class="line">				System.out.println(rs.getString(&quot;name&quot;)+&quot;==&quot;+rs.getInt(&quot;money&quot;));</span><br><span class="line">			&#125;*/</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			// 回滚事务</span><br><span class="line">			try &#123;</span><br><span class="line">				conn.rollback();</span><br><span class="line">			&#125; catch (SQLException e1) &#123;</span><br><span class="line">				// TODO Auto-generated catch block</span><br><span class="line">				e1.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps, rs);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>事务</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">conn.setAutoCommit(false)来关闭自动提交的设置</span><br><span class="line">conn.commit(); 提交事务</span><br><span class="line">conn.rollback(); 回滚事务</span><br></pre></td></tr></table></figure>
<p>记住:<br>什么是事务,事务有什么用处,事务怎么用.</p>
<p>事务的特点:</p>
<ol>
<li>原子性: 指的是事务中包含的逻辑,不可以分割(事务中逻辑不可分)</li>
<li>一致性: 事务执行前和执行后,保持数据的完整性一致(执行前后,数据保持一致)</li>
<li>隔离性: 事务在执行期间不受到其他事务的影响(隔离不受影响)</li>
<li>持久性: 事务执行结束,提交或回滚,数据都应该持久化到数据中(数据持久化在数据中)</li>
</ol>
<p>安全问题</p>
<ol>
<li>读问题<br>脏读,不可重复读,幻读</li>
<li>写问题<br>丢失更新,解决丢失更新的方法,一悲观锁和乐观锁</li>
</ol>
<p>隔离级别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Read Uncommitted 读未提交</span><br><span class="line">Read Committed 读已提交</span><br><span class="line">Repeatable Read 重复读</span><br><span class="line">Serializable 可串行化</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-340d7d7c9fa17391.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="隔离级别"></p>
<p>脏读:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d9106d65bda58b29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-5ddc6d9143e31aff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p>一条连接:<br><img src="https://upload-images.jianshu.io/upload_images/11158618-e64e5a5deeb0b1c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p>另一条连接:<br><img src="https://upload-images.jianshu.io/upload_images/11158618-f8bbe6bf9b1edaeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">use bank;</span><br><span class="line">start transaction;</span><br><span class="line">select * from account;</span><br><span class="line">select * from account;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">use bank;</span><br><span class="line">start transaction;</span><br><span class="line">update account set money = money + 300 where id = 1;</span><br><span class="line">commit;</span><br></pre></td></tr></table></figure>
<p>读未提交</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-c450391d3938cb67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<blockquote>
<p>例子</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-cdb604cd503e7150.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p>这里查询,然后再看看数据库中的表:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f7bf143f903f5ea4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="刷新后"></p>
<p>命令提交:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-9b76968719b44c3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令提交"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-88e62f1497ebc2e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="查询数据"></p>
<blockquote>
<p>不可重复读的问题</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-71768cebcf17d9c3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<h2 id="Repeatable-Read重复读"><a href="#Repeatable-Read重复读" class="headerlink" title="Repeatable Read重复读"></a><code>Repeatable Read</code>重复读</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-36554b379444364c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-2a54bec6bdd2dc42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d0f4b3b9da1054f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="表"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-a4e5431c4351fb69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<h2 id="可串化"><a href="#可串化" class="headerlink" title="可串化"></a>可串化</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-1d7f007806a44df5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-748f14ac618bd972.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<blockquote>
<p>读未提交,引发脏读,读已提交解决脏读,引发不可重复读, 可重复读解决脏读解决了脏读,不可重复读,</p>
</blockquote>
<p>asss</p>
<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>读:<br>脏读 不可重读读 幻读</p>
<p>脏读:<br>一个事务读到另外一个事务还未提交的数据</p>
<p>不可重复读:<br>一个事务读到另外一个事务提交的数据,导致前后两次查询结果不一致</p>
<p>幻读:<br>一个事务读到了另外一个事务已提交的插入的数据,导致多次查询结果不一致.</p>
<p>读未提交,<br>会导致丢失更新</p>
<p>读已提交,<br>能够屏蔽 脏读的现象,但是引发不可重复读</p>
<h2 id="事务的安全隐患脏读"><a href="#事务的安全隐患脏读" class="headerlink" title="事务的安全隐患脏读"></a>事务的安全隐患脏读</h2><p>隔离级别:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">read uncommitted 读未提交</span><br><span class="line">read committed 读已提交</span><br><span class="line">repeatable read 重复读</span><br><span class="line">serializable 可串化</span><br></pre></td></tr></table></figure>
<p><code>mysql</code>默认的是重复读.</p>
<p>设置A窗口的隔离级别为 读未提交 </p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-e9a7fb886f61a2b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="a"></p>
<p>两个窗口都分别开启事务</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-abc1c8eabc71877b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="脏读"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-ab4e57154f848904.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="提交后"></p>
<p>读未提交是一个事务可以读取到另一个事务还没有提交的数据,会引发脏读现象,读取到的是数据库内存中的数据,并不是真正的磁盘上的数据.</p>
<p>还未提交时,数据库内存中的数据是不会改变的,只有到commit提交后,数据库中的数据才会提交,从而读取真正在磁盘上的数据.</p>
<h2 id="Read-Committed读已提交"><a href="#Read-Committed读已提交" class="headerlink" title="Read Committed读已提交"></a><code>Read Committed</code>读已提交</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-c7b28ae1809f056d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-cb00c0352e7e6afa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p>在a发生了变化,屏蔽了脏读,引发不可重复读,读已提交.读已提交,在a窗口设置的是读已提交,两边开启事务.在b窗口更新操作.</p>
<p>在a窗口查询结果不一致,一次是在b窗口提交事务之前,一次的提交之后.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-ef698665a32c12f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="前后"></p>
<p>引发了不可重复读</p>
<p>不可重复读,一个事务读取到了另一个事务提交的数据,导致前后两次查询结果不一致.</p>
<p>会造成问题是前后读取到的结果不一样,发生了不可重复读,就是不可以 重复读取, 就是不能执行多次读取, 否则会导致结果不一致, 这下好了, 读取已提交导致了 重复读取, 结果还不一致, 就出现了叫 不可重复读 现象.</p>
<h2 id="安全隐患-可重复读"><a href="#安全隐患-可重复读" class="headerlink" title="安全隐患_可重复读"></a>安全隐患_可重复读</h2><p><code>Repeatable Read</code>重复读, 重复读就是mysql默认的隔离级别,可以让食物在自己的会话中重复读取数据,并且不会出现结果不一致的现象, 就算其他事务提交了, 也还是会在窗口中出现以前的数据, 这就是可重复读了.</p>
<p>重复读, 就是让它可以重复查询, 结果还是和以前一样的效果出现.</p>
<h2 id="设置重复读"><a href="#设置重复读" class="headerlink" title="设置重复读"></a>设置重复读</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-e0f2d760fae4d676.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="重复读"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-a536c4ba1f1d6304.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="重复读"></p>
<h2 id="安全隐患-可串行化"><a href="#安全隐患-可串行化" class="headerlink" title="安全隐患 _可串行化"></a>安全隐患 _可串行化</h2><p>幻读:什么是幻读呢?<br>就是一个事务读到另一个事务已提交的插入的数据,会导致多次查询结果不一致.就是幻读,是不是幻读理解为我幻想了, 事务已提交的插入数据, 导致幻想了,(幻读) 导致每次查询结果不一样.</p>
<p>事务已提交,多次查询结果不一致.</p>
<p>幻读-&gt;<code>Serializable</code>可串行化</p>
<p>该事务的级别是最高的事务级别,我是可串行化,是最高的.可以解决如下小弟的问题,如脏读,不可重复读,幻读,因为我是可串行化,是大佬,但作为大佬,还是会有缺点的.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-9fef7bc147a7897b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-9c3847fe44648faf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-567b038b4c0f658c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p>是的,a提交才动.现在我们让b先开启事务.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-1f69b3e6719fe3ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-5439323e10ed52ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-4e07621f4afff961.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="提交了"></p>
<p>可串行化, 谁先打开事务,就谁有权利,这个隔离级别,先打开就有权利让别人等着,等先打开事务的那个家伙,提交或者回滚后,才能进行,这种级别是用得比较少的,因为容易导致性能上效率低下.</p>
<p>隔离级别有四个哦</p>
<ol>
<li>读未提交</li>
<li>读已提交</li>
<li>可重复读</li>
<li>可串行化</li>
</ol>
<p>如果按照效率划分,从高到低,排个名次如下:</p>
<ol>
<li>读未提交 -&gt; 脏读</li>
<li>读已提交 -&gt; 不可重复读</li>
<li>可重复读 -&gt; 解决重复读</li>
<li>可串行化 -&gt; 以上都是我小弟来着</li>
</ol>
<p>按照拦截程度来划分,从高到底,排名如下:</p>
<ol>
<li>可串行化 -&gt; 我是大哥</li>
<li>可重复读 -&gt; 我是二哥</li>
<li>读已提交 -&gt; 我是三弟</li>
<li>读未提交 -&gt; 我是小弟</li>
</ol>
<h2 id="事务-隔离级别小结"><a href="#事务-隔离级别小结" class="headerlink" title="事务_隔离级别小结"></a>事务_隔离级别小结</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 使用事务</span><br><span class="line">conn.setAutoCommit(false);</span><br><span class="line">// 提交</span><br><span class="line">conn.commit();</span><br><span class="line">// 回滚</span><br><span class="line">conn.rollback();</span><br></pre></td></tr></table></figure>
<p>事务只是针对连接对象.事务是会自动提交的.</p>
<p>安全隐患和隔离级别</p>
<p>安全隐患: 读的安全隐患和写的安全隐患</p>
<p>读:<br>脏读,读到未提交的数据,一个事务读到了另一个事务未提交的数据;<br>不可重复读,就是一个事务读到了另一个事务已经提交的数据,导致前后两次查询的结果不一致;<br>幻读,就是一个事务读到了另一个事务添加的数据,导致前后查询结果不一致.</p>
<p>写: 丢失更新…</p>
<blockquote>
<p>隔离级别</p>
</blockquote>
<p>读未提交,导致脏读<br>读已提交,解决脏读,导致不可重复读<br>可重复读,解决脏读和不可重复读,导致幻读<br>可串行化,解决脏读,不可重复读,幻读</p>
<p>默认的mysql是可重复读,oracle默认是读已提交</p>
<h2 id="写的问题-丢失更新"><a href="#写的问题-丢失更新" class="headerlink" title="写的问题_丢失更新"></a>写的问题_丢失更新</h2><p>丢失更新</p>
<ol>
<li>乐观锁</li>
<li>悲观锁</li>
</ol>
<p>安全问题包含 读的问题和写的问题</p>
<blockquote>
<p>事务的特性是什么?<br>原子性,一致性,隔离性,持久性</p>
</blockquote>
<h2 id="写的问题-丢失更新-1"><a href="#写的问题-丢失更新-1" class="headerlink" title="写的问题_丢失更新"></a>写的问题_丢失更新</h2><p><img src="https://upload-images.jianshu.io/upload_images/11158618-28e95bc6dd2e238d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f849cba99e5c3b8e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p>b窗口没有提交.等待提交中:</p>
<p>案例控制面板,我的a:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-3dd1e1ddb5f434d6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="a的"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-4535bfedba1a991d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="b的"></p>
<p>哭了,这是设置默认的重复读啊!</p>
<h2 id="丢失更新的问题"><a href="#丢失更新的问题" class="headerlink" title="丢失更新的问题"></a>丢失更新的问题</h2><blockquote>
<p>听说丢失更新</p>
</blockquote>
<p>a事务和b事务同时查询一个表,a开始修改并提交<code>name</code>字段的名字,然后b事务开始修改该行的<code>money</code>的字段,如果b事务提交,那么之前a事务修改的名字没有了,变回去了哦,当然b事务回滚,也同样导致a事务更新没有了哦.回滚也会把之前b事务的最初的数据还原.</p>
<p>这里的情况处理序列化级别外,就是可串行化级别大佬哦!</p>
<blockquote>
<p>解决丢失更新的方法</p>
</blockquote>
<ol>
<li>悲观锁</li>
<li>乐观锁</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select * from account;</span><br></pre></td></tr></table></figure>
<p>悲观锁的态度,它是悲观的态度,它是一定会丢失更新,它的想法是我一定会出错.</p>
<p>而乐观锁,它的态度是我一定不会丢失更新.</p>
<blockquote>
<p>悲观锁</p>
</blockquote>
<p>数据库的锁机制,排他锁<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select * from account for update;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-7d92e61a7b2a6340.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-b19bc37d5e4ec399.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="命令行"></p>
<blockquote>
<p>丢失更新的问题</p>
</blockquote>
<p>不考虑隔离性,产生写入数据导致的问题为丢失更新的问题,两个事务同时对某一条记录做修改,然后会导致丢失更新的问题.</p>
<p>a,b两个事务同时获取一条数据,同时做了修改,a事务修改后,提交了事务,b事务修改后,不管是提交还是回滚,都会对数据发生影响.</p>
<p>悲观锁记住用了这句:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from account for update;</span><br><span class="line">for update;</span><br><span class="line">数据库锁机制,排他锁</span><br></pre></td></tr></table></figure>
<blockquote>
<p>乐观锁</p>
</blockquote>
<p>a事务先提交,数据库版本<code>version</code>变为1,b事务在提交的时候,比对数据库<code>version</code>和自己的<code>version</code>是不一样的,不允许提交,要先更新.</p>
<p>a提交的时候版本变为1,b提交的时候,发现版本不一致就无法提交,要进行更新后提交.</p>
<h2 id="数据库连接池"><a href="#数据库连接池" class="headerlink" title="数据库连接池"></a>数据库连接池</h2><p>什么是连接池,连接池的作用是什么,自定义连接池,开源的连接池?</p>
<p>那么什么是数据库连接池?</p>
<p>数据库连接池是Java程序和数据连接的中介媒介,以前一个Java程序对应一个连接,然后就可以连接到数据了,可以一旦多了呢?</p>
<p>就有人发明了数据库连接池,可以一下连接多个,但是是有限制的,一旦多了,就会扩容,额外增加3到5个,不会增幅太大,也有最大值的限制.</p>
<blockquote>
<p>数据库的连接对象 创建工作 比较消耗性能<br>一开始在内存中会开辟一块空间, 用于是 这个数据库连接池的空间, 可以在池子里放置很多个连接对象, 数据库连接池里有很多个连接对象, 后面需要连接的话会直接从池子里面去, 就不用自己去创建连接了, 因为数据库的连接对象创建工作是比较耗时的, 使用完, 数据库连接池中的 连接对象 ,是要进行归还的, 确保连接对象可以循环连接使用.</p>
</blockquote>
<blockquote>
<p>创建数据库的连接池</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">// 简单使用数据库连接池</span><br><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">public class TestPool &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testPool() &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		MyDataSource dataSource = new MyDataSource();</span><br><span class="line">		try &#123;</span><br><span class="line">	</span><br><span class="line">			conn = dataSource.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = &quot;insert into account values (null, &apos;xilali&apos;, 10)&quot;;</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			try &#123;</span><br><span class="line">				ps.close();</span><br><span class="line">			&#125; catch (SQLException e) &#123;</span><br><span class="line">				// TODO Auto-generated catch block</span><br><span class="line">				e.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			// 归还连接</span><br><span class="line">			dataSource.addBack(conn);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-ab552bda614279ac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<blockquote>
<p>自定义数据库连接池</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List &lt;Connection&gt; list = new ArrayList&lt;Connection&gt;();</span><br></pre></td></tr></table></figure>
<p>解决自定义数据库连接池问题</p>
<p>因为多了一个<code>addBack</code>方法,要记住这个方法,且不能用面向接口编程.修改<code>close</code>方法,改成不是关闭而是归还连接对象.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class MyDataSource implements DataSource &#123;</span><br><span class="line"> // 创建连接池</span><br><span class="line">List&lt;Connection&gt; list = new ArrayList&lt;Connection&gt;();</span><br><span class="line"> public MyDataSource()&#123;</span><br><span class="line">  for(int i = 0; i &lt; 10; i++)&#123;</span><br><span class="line">   Connection conn = JDBCUtil.getConn();</span><br><span class="line">   list.add(conn);</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> @Override</span><br><span class="line"> public Connection getConnection() throws SQLException &#123;</span><br><span class="line">   if(list.size() == 0)&#123;</span><br><span class="line">     for(int i=0; i&lt;5; i++) &#123;</span><br><span class="line">      Connection conn = JDBCUtil.getConn();</span><br><span class="line">       list.add(conn);</span><br><span class="line">     &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    Connection conn = list.remove(0);</span><br><span class="line">     return conn;</span><br><span class="line">  &#125;</span><br><span class="line">  // 用完后归还 </span><br><span class="line">  public void addBack(Connection conn)&#123;</span><br><span class="line">    list.add(conn);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class TestPool&#123;</span><br><span class="line"> @Test</span><br><span class="line">  public void testPool()&#123;</span><br><span class="line">   Connection conn = null;</span><br><span class="line">   PreparedStatement ps = null;</span><br><span class="line">   MyDataSource dataSource = new MyDataSource();</span><br><span class="line">  try &#123;</span><br><span class="line">   conn = dataSource.getConnection();</span><br><span class="line">   String sql = &quot;insert into account values(null, &apos;dashu&apos;, 10)&quot;;</span><br><span class="line">   ps = conn.prepareStatement(sql);</span><br><span class="line">   ps.executeUpdate();</span><br><span class="line"> </span><br><span class="line">   &#125;catch(SQLException e)&#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">   &#125;finally&#123;</span><br><span class="line">    try&#123;</span><br><span class="line">    ps.close();</span><br><span class="line">    &#125;catch(SQLException e)&#123;</span><br><span class="line">     e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    // 归还连接</span><br><span class="line">    dataSource.addBack(conn);</span><br><span class="line">   &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如何扩展方法</p>
</blockquote>
<p>装饰者模式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">public interface Waiter &#123;</span><br><span class="line">	void service();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">public class Waitress implements Waiter &#123;</span><br><span class="line">	@Override</span><br><span class="line">	public void service() &#123;</span><br><span class="line">		System.out.println(&quot;在服务...&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">public class MainTest &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Waiter waiter = new Waitress();</span><br><span class="line">		waiter.service();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>装饰者模式:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">public class WaitressWrap implements Waiter &#123;</span><br><span class="line">	</span><br><span class="line">	Waiter watier = null;</span><br><span class="line">	public WaitressWrap(Waiter watier) &#123;</span><br><span class="line">		this.watier = watier;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void service() &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		System.out.println(&quot;微笑&quot;);</span><br><span class="line">		watier.service();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.test;</span><br><span class="line"></span><br><span class="line">public class MainTest &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		/*Waiter waiter = new Waitress();</span><br><span class="line">		waiter.service();*/</span><br><span class="line">		WaitressWrap waitressWrap = new WaitressWrap(new Waitress());</span><br><span class="line">		waitressWrap.service();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>分析</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Waiter -&gt; service()</span><br><span class="line"></span><br><span class="line">Waitress -&gt; service()</span><br><span class="line"></span><br><span class="line">WaitressWrap -&gt; service()</span><br><span class="line"></span><br><span class="line">Waiter waiter;</span><br><span class="line">public Waitress Wrap(Waiter waiter)&#123;</span><br><span class="line"> this.waiter = watier;</span><br><span class="line">&#125;</span><br><span class="line">void service()&#123;</span><br><span class="line"> syso</span><br><span class="line"> waiter.service();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>装饰者模式</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-1afb55e7867fc95e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-21b7e26bb44e8949.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="代码"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.io.PrintWriter;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.SQLFeatureNotSupportedException;</span><br><span class="line">import java.util.ArrayList;</span><br><span class="line">import java.util.List;</span><br><span class="line">import java.util.logging.Logger;</span><br><span class="line"></span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line">// 这是一个数据库连接池</span><br><span class="line">// 一开始 先往连接池放入10个连接</span><br><span class="line">public class MyDataSource implements DataSource&#123;</span><br><span class="line">	</span><br><span class="line">	// 创建连接池</span><br><span class="line">	List&lt;Connection&gt; list = new ArrayList&lt;Connection&gt;();</span><br><span class="line">	public MyDataSource() &#123;</span><br><span class="line">		// 10个连接</span><br><span class="line">		for(int i = 0; i &lt; 10; i++) &#123;</span><br><span class="line">			Connection conn = JDBCUtil.getConn();</span><br><span class="line">			list.add(conn);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 该连接池对外公布的获取连接的方法</span><br><span class="line">	@Override</span><br><span class="line">	public Connection getConnection() throws SQLException &#123;</span><br><span class="line">		// 来拿连接的时候,看看,池子里面有没有</span><br><span class="line">		if(list.size() == 0) &#123;</span><br><span class="line">			for(int i = 0; i &lt; 5; i++) &#123;</span><br><span class="line">				Connection  conn = JDBCUtil.getConn();</span><br><span class="line">				list.add(conn);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		// 那一个 给连接 如果有10个,0,1,2,3,-&gt;10 这样给</span><br><span class="line">		// 确保给</span><br><span class="line">		// 移除就给了嘛</span><br><span class="line">		// remove(0) -&gt; 移除第一个</span><br><span class="line">		Connection conn = list.remove(0);</span><br><span class="line">		// 把对象抛出去的时候,对这个对象进行包装</span><br><span class="line">		</span><br><span class="line">		Connection connection = new ConnectionWrap(conn,list);</span><br><span class="line">		</span><br><span class="line">		return connection;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	// 用完后记得归还</span><br><span class="line">	public void addBack(Connection conn) &#123;</span><br><span class="line">		list.add(conn);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public PrintWriter getLogWriter() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public int getLoginTimeout() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Logger getParentLogger() throws SQLFeatureNotSupportedException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setLogWriter(PrintWriter arg0) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setLoginTimeout(int arg0) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean isWrapperFor(Class&lt;?&gt; arg0) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public &lt;T&gt; T unwrap(Class&lt;T&gt; arg0) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Connection getConnection(String arg0, String arg1) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-c7889696705c734f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="代码"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.sql.Array;</span><br><span class="line">import java.sql.Blob;</span><br><span class="line">import java.sql.CallableStatement;</span><br><span class="line">import java.sql.Clob;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DatabaseMetaData;</span><br><span class="line">import java.sql.NClob;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLClientInfoException;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.SQLWarning;</span><br><span class="line">import java.sql.SQLXML;</span><br><span class="line">import java.sql.Savepoint;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line">import java.sql.Struct;</span><br><span class="line">import java.util.List;</span><br><span class="line">import java.util.Map;</span><br><span class="line">import java.util.Properties;</span><br><span class="line">import java.util.concurrent.Executor;</span><br><span class="line"></span><br><span class="line">public class ConnectionWrap implements Connection&#123;</span><br><span class="line">	</span><br><span class="line">	Connection connection = null;</span><br><span class="line">	</span><br><span class="line">	List&lt;Connection&gt; list;</span><br><span class="line">	public ConnectionWrap(Connection connection,List&lt;Connection&gt; list) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.connection = connection;</span><br><span class="line">		this.list = list;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void close() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		//connection.close();</span><br><span class="line">		System.out.println(&quot;有人归还连接对象了,归还之前&quot;+list.size());</span><br><span class="line">		list.add(connection);</span><br><span class="line">		System.out.println(&quot;有人归还连接对象了,归还之后&quot;+list.size());</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return connection.prepareStatement(sql);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean isWrapperFor(Class&lt;?&gt; iface) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public &lt;T&gt; T unwrap(Class&lt;T&gt; iface) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void abort(Executor executor) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void clearWarnings() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void commit() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Array createArrayOf(String typeName, Object[] elements) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Blob createBlob() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Clob createClob() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public NClob createNClob() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public SQLXML createSQLXML() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Statement createStatement() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)</span><br><span class="line">			throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Struct createStruct(String typeName, Object[] attributes) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean getAutoCommit() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getCatalog() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Properties getClientInfo() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getClientInfo(String name) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public int getHoldability() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public DatabaseMetaData getMetaData() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public int getNetworkTimeout() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String getSchema() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public int getTransactionIsolation() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Map&lt;String, Class&lt;?&gt;&gt; getTypeMap() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public SQLWarning getWarnings() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean isClosed() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean isReadOnly() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public boolean isValid(int timeout) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return false;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String nativeSQL(String sql) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public CallableStatement prepareCall(String sql) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,</span><br><span class="line">			int resultSetHoldability) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)</span><br><span class="line">			throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,</span><br><span class="line">			int resultSetHoldability) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void releaseSavepoint(Savepoint savepoint) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void rollback() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void rollback(Savepoint savepoint) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setAutoCommit(boolean autoCommit) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setCatalog(String catalog) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setClientInfo(Properties properties) throws SQLClientInfoException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setClientInfo(String name, String value) throws SQLClientInfoException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setHoldability(int holdability) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setReadOnly(boolean readOnly) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Savepoint setSavepoint() throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Savepoint setSavepoint(String name) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setSchema(String schema) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setTransactionIsolation(int level) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void setTypeMap(Map&lt;String, Class&lt;?&gt;&gt; map) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d5023746f361db32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-8078c5a665e3ccce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="代码"></p>
<blockquote>
<p>数据库连接池_DBCP</p>
</blockquote>
<p>DBCP开源连接池<br>C3P0,什么是C3P0,怎么用</p>
<p>DBCP为数据库连接池,是java数据库连接池的一种是Apache开发的,通过数据库连接池可以让程序自动管理数据库的连接.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DataBase Connection Pool数据库连接池</span><br></pre></td></tr></table></figure>
<p>C3P0也是一种开源的连接池,实现了数据库和JNDI绑定,使用它的开源项目:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Spring, Hibernate</span><br></pre></td></tr></table></figure>
<blockquote>
<p>怎么用DBCP</p>
</blockquote>
<ol>
<li>导入jar包<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">commons-dbcp.jar</span><br><span class="line">commons-pool.jar</span><br></pre></td></tr></table></figure>
</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// 不使用配置文件</span><br><span class="line">BasicDataSource dataSource = new BasicDataSource();</span><br><span class="line">dataSource.setDriverClassName(&quot;com.mysql.jdbc.Driver&quot;);</span><br><span class="line">dataSource.setUrl(&quot;jdbc:mysql://loclhost/users&quot;);</span><br><span class="line">dataSource.setUsername(&quot;root&quot;);</span><br><span class="line">dataSource.setPassword(&quot;root&quot;);</span><br><span class="line"></span><br><span class="line">conn = dataSource.getConnection();</span><br><span class="line">String sql = &quot;select * from user&quot;;</span><br><span class="line">pstmt = conn.prepareStatement(sql);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>DBCP连接数据库使用</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbcp;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbcp.BasicDataSource;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line"></span><br><span class="line">// 这个连接池要连接数据库, 账号,密码</span><br><span class="line">public class DBCPDemo &#123;</span><br><span class="line">	// ctrl + 2 f</span><br><span class="line">	private Connection conn;</span><br><span class="line">	private PreparedStatement ps;</span><br><span class="line"></span><br><span class="line">	@Test</span><br><span class="line">	public void testDBCP01()&#123;</span><br><span class="line">		// 数据库的连接池, 作用创建和连接</span><br><span class="line">		</span><br><span class="line">		try &#123;</span><br><span class="line">			// 构建数据源对象</span><br><span class="line">			BasicDataSource dataSource = new BasicDataSource();</span><br><span class="line">			// 连接什么数据库,用户名和密码</span><br><span class="line">			dataSource.setDriverClassName(&quot;com.mysql.jdbc.Driver&quot;);</span><br><span class="line">			dataSource.setUrl(&quot;jdbc:mysql://localhost/bank&quot;);</span><br><span class="line">			dataSource.setUsername(&quot;你的账户&quot;);</span><br><span class="line">			dataSource.setPassword(&quot;你的密码&quot;);</span><br><span class="line">			</span><br><span class="line">			// 得到连接对象</span><br><span class="line">			conn = dataSource.getConnection();</span><br><span class="line">			</span><br><span class="line">			String sql = &quot;insert into account values(null, ?, ?)&quot;;</span><br><span class="line">			</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			</span><br><span class="line">			ps.setString(1, &quot;dashucoding&quot;);</span><br><span class="line">			ps.setInt(2, 10000);</span><br><span class="line">			</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上是不使用配置文件的情况.</p>
<blockquote>
<p>DBCP使用配置文件方式</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-5d7951abe03baa0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-36d98c1365978dae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbcp;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbcp.BasicDataSource;</span><br><span class="line">import org.apache.commons.dbcp.BasicDataSourceFactory;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line"></span><br><span class="line">public class DBCPDemo02 &#123;</span><br><span class="line"></span><br><span class="line">	@Test</span><br><span class="line">	public void testDBCP02() &#123;</span><br><span class="line">		/*BasicDataSource dataSource = new BasicDataSource();</span><br><span class="line">		dataSource.setConnectionProperties(&quot;dbcpconfig.properties&quot;);*/</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line"></span><br><span class="line">		// 数据库的连接池, 作用创建和连接</span><br><span class="line">		try &#123;</span><br><span class="line">			BasicDataSourceFactory factory = new BasicDataSourceFactory();</span><br><span class="line">			Properties properties = new Properties();</span><br><span class="line">			InputStream is = new FileInputStream(&quot;src//dbcpconfig.properties&quot;);</span><br><span class="line">			properties.load(is);</span><br><span class="line">			DataSource dataSource = factory.createDataSource(properties);</span><br><span class="line"></span><br><span class="line">			// 得到连接对象</span><br><span class="line">			conn = dataSource.getConnection();</span><br><span class="line"></span><br><span class="line">			String sql = &quot;insert into account values(null, ?, ?)&quot;;</span><br><span class="line"></span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line"></span><br><span class="line">			ps.setString(1, &quot;liuliuliu&quot;);</span><br><span class="line">			ps.setInt(2, 10000);</span><br><span class="line"></span><br><span class="line">			ps.executeUpdate();</span><br><span class="line"></span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="数据库连接池-C3P0"><a href="#数据库连接池-C3P0" class="headerlink" title="数据库连接池_C3P0"></a>数据库连接池_C3P0</h2><blockquote>
<p>不使用配置方式</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">拷贝jar c3p0...jar</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Just put the jar file [lib/c3p0-0.9.1.2.jar] in your application&apos;s effective CLASSPATH</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ComboPooledDataSource cpds = new ComboPooledDataSource();</span><br><span class="line">cpds.setDriverClass( &quot;org.postgresql.Driver&quot; ); //loads the jdbc driver            </span><br><span class="line">cpds.setJdbcUrl( &quot;jdbc:postgresql://localhost/testdb&quot; );</span><br><span class="line">cpds.setUser(&quot;dbuser&quot;);                                  </span><br><span class="line">cpds.setPassword(&quot;dbpassword&quot;);</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-4613be7b7ae6b406.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.c3p0;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbcp.BasicDataSource;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class C3P0Demo &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testC3P0() &#123;</span><br><span class="line"></span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 创建dataSource</span><br><span class="line">			ComboPooledDataSource dataSource = new ComboPooledDataSource();</span><br><span class="line">			</span><br><span class="line">			dataSource.setDriverClass(&quot;com.mysql.jdbc.Driver&quot;);        </span><br><span class="line">			dataSource.setJdbcUrl( &quot;jdbc:mysql://localhost/bank&quot; );</span><br><span class="line">			dataSource.setUser(&quot;root&quot;);                                  </span><br><span class="line">			dataSource.setPassword(&quot;admin&quot;);  </span><br><span class="line">			</span><br><span class="line"></span><br><span class="line">			// 得到连接对象</span><br><span class="line">			conn = dataSource.getConnection();</span><br><span class="line"></span><br><span class="line">			String sql = &quot;insert into account values(null, ?, ?)&quot;;</span><br><span class="line"></span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line"></span><br><span class="line">			ps.setString(1, &quot;aaa&quot;);</span><br><span class="line">			ps.setInt(2, 10000);</span><br><span class="line"></span><br><span class="line">			ps.executeUpdate();</span><br><span class="line"></span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>C3P0使用配置文件的方式</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-4f0bb33cbb0d382b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图片"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d7050d50a84bb14f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图片"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f05618d59e909ffc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p><code>c3p0-config.xml file</code>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;c3p0-config&gt;</span><br><span class="line">  &lt;default-config&gt;</span><br><span class="line">    &lt;property name=&quot;driverClass&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;jdbcUrl&quot;&gt;jdbc:mysql://localhost/bank&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;user&quot;&gt;你的账户&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;password&quot;&gt;你的密码&lt;/property&gt;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    &lt;property name=&quot;initialPoolSize&quot;&gt;10&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;maxIdleTime&quot;&gt;30&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;maxPoolSize&quot;&gt;100&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;minPoolSize&quot;&gt;10&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;maxStatements&quot;&gt;200&lt;/property&gt;</span><br><span class="line"></span><br><span class="line">  &lt;/default-config&gt;</span><br><span class="line">  </span><br><span class="line">&lt;/c3p0-config&gt;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-be73f8e8b3fdc4f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.c3p0;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line">// 使用配置文件</span><br><span class="line">public class C3P0Demo02 &#123;</span><br><span class="line"></span><br><span class="line">	@Test</span><br><span class="line">	public void testC3P0() &#123;</span><br><span class="line"></span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 配置文件 new了一个对象</span><br><span class="line">			ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line">			</span><br><span class="line">			// 得到连接对象</span><br><span class="line">			conn = dataSource.getConnection();</span><br><span class="line"></span><br><span class="line">			String sql = &quot;insert into account values(null, ?, ?)&quot;;</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			ps.setString(1, &quot;bbb&quot;);</span><br><span class="line">			ps.setInt(2, 10000);</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line"></span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以弄oracle:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- This app is massive! --&gt;</span><br><span class="line">  &lt;named-config name=&quot;oracle&quot;&gt; </span><br><span class="line">    &lt;property name=&quot;acquireIncrement&quot;&gt;50&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;initialPoolSize&quot;&gt;100&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;minPoolSize&quot;&gt;50&lt;/property&gt;</span><br><span class="line">    &lt;property name=&quot;maxPoolSize&quot;&gt;1000&lt;/property&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- intergalactoApp adopts a different approach to configuring statement caching --&gt;</span><br><span class="line">    &lt;property name=&quot;maxStatements&quot;&gt;0&lt;/property&gt; </span><br><span class="line">    &lt;property name=&quot;maxStatementsPerConnection&quot;&gt;5&lt;/property&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- he&apos;s important, but there&apos;s only one of him --&gt;</span><br><span class="line">    &lt;user-overrides user=&quot;master-of-the-universe&quot;&gt; </span><br><span class="line">      &lt;property name=&quot;acquireIncrement&quot;&gt;1&lt;/property&gt;</span><br><span class="line">      &lt;property name=&quot;initialPoolSize&quot;&gt;1&lt;/property&gt;</span><br><span class="line">      &lt;property name=&quot;minPoolSize&quot;&gt;1&lt;/property&gt;</span><br><span class="line">      &lt;property name=&quot;maxPoolSize&quot;&gt;5&lt;/property&gt;</span><br><span class="line">      &lt;property name=&quot;maxStatementsPerConnection&quot;&gt;50&lt;/property&gt;</span><br><span class="line">    &lt;/user-overrides&gt;</span><br><span class="line">  &lt;/named-config&gt;</span><br></pre></td></tr></table></figure>
<p><code>oracle</code>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ComboPooledDataSource dataSource = new ComboPooledDataSource();</span><br></pre></td></tr></table></figure>
<h2 id="DBUtils"><a href="#DBUtils" class="headerlink" title="DBUtils"></a><code>DBUtils</code></h2><p>什么是DBUtils呢?怎么用呢?</p>
<p>优化数据库连接,使用C3P0:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DriverManager;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class JDBCUtil02 &#123;</span><br><span class="line">	</span><br><span class="line">	static ComboPooledDataSource dataSource = null;</span><br><span class="line"></span><br><span class="line">	static &#123;</span><br><span class="line">		dataSource = new ComboPooledDataSource();</span><br><span class="line">	&#125;</span><br><span class="line">	/**</span><br><span class="line">	 * 获取连接对象</span><br><span class="line">	 * @return</span><br><span class="line">	 * @throws SQLException </span><br><span class="line">	 */</span><br><span class="line">	public static Connection getConn() throws SQLException&#123;</span><br><span class="line">		</span><br><span class="line">		return dataSource.getConnection();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 释放资源</span><br><span class="line">	 * @param conn</span><br><span class="line">	 * @param st</span><br><span class="line">	 * @param rs</span><br><span class="line">	 */</span><br><span class="line">	public static void release(Connection conn , Statement st , ResultSet rs)&#123;</span><br><span class="line">		closeRs(rs);</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line">	public static void release(Connection conn , Statement st)&#123;</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	private static void closeRs(ResultSet rs)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(rs != null)&#123;</span><br><span class="line">				rs.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			rs = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeSt(Statement st)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(st != null)&#123;</span><br><span class="line">				st.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			st = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeConn(Connection conn)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(conn != null)&#123;</span><br><span class="line">				conn.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			conn = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>DBUtils优化增删改查方法</p>
</blockquote>
<p>导包,两行代码交你增删改查,6666!</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br></pre></td></tr></table></figure>
<p>两行.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-46c9098f2b9ee519.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-c8fa75e19f31660a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbutils;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class TestDBUtils &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert() throws SQLException &#123;</span><br><span class="line">//		ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line"></span><br><span class="line">		// dbutils 简化了CRUD的代码</span><br><span class="line">		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">		</span><br><span class="line">		/*</span><br><span class="line">		 * queryRunner.update(sql);</span><br><span class="line">		 * 增删改</span><br><span class="line">		 * queryRunner.query(sql, rsh);</span><br><span class="line">		 * 查</span><br><span class="line">		 * */</span><br><span class="line">		</span><br><span class="line">		queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbutils;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class TestDBUtils &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert() throws SQLException &#123;</span><br><span class="line">//		ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line"></span><br><span class="line">		// dbutils 简化了CRUD的代码</span><br><span class="line">		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">		</span><br><span class="line">		/*</span><br><span class="line">		 * queryRunner.update(sql);</span><br><span class="line">		 * 增删改</span><br><span class="line">		 * queryRunner.query(sql, rsh);</span><br><span class="line">		 * 查</span><br><span class="line">		 * */</span><br><span class="line">		</span><br><span class="line">		queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br><span class="line">		queryRunner.update(&quot;delete from account where id = ?&quot;, 8);</span><br><span class="line">		queryRunner.update(&quot;update account set money = ? where id = ?&quot;, 0, 10);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>查询</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-7528888cd0b13a3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbutils;</span><br><span class="line"></span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.ResultSetHandler;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Account;</span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class TestDBUtils &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert() throws SQLException &#123;</span><br><span class="line">//		ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line"></span><br><span class="line">		// dbutils 简化了CRUD的代码</span><br><span class="line">		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">		// 查询的是一个Bean对象</span><br><span class="line">		// new 匿名实现类-&gt;new接口的匿名实现类</span><br><span class="line">		</span><br><span class="line">		Account account = queryRunner.query(&quot;select * from account where id = ?&quot;, new ResultSetHandler&lt;Account&gt;() &#123;</span><br><span class="line">			// 去执行查询,查询到的数据在result里面,然后调用handle方法,用户手动去封装</span><br><span class="line">			@Override</span><br><span class="line">			public Account handle(ResultSet rs) throws SQLException &#123;</span><br><span class="line">				// TODO Auto-generated method stub</span><br><span class="line">				Account account = new Account();</span><br><span class="line">				</span><br><span class="line">				while(rs.next()) &#123;</span><br><span class="line">					String name = rs.getString(&quot;name&quot;);</span><br><span class="line">					int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">					account.setName(name);</span><br><span class="line">					account.setMoney(money);</span><br><span class="line">				&#125;</span><br><span class="line">				return account;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;, 1);</span><br><span class="line">		System.out.println(account.toString());</span><br><span class="line">		</span><br><span class="line">		/*</span><br><span class="line">		 * queryRunner.update(sql);</span><br><span class="line">		 * 增删改</span><br><span class="line">		 * queryRunner.query(sql, rsh);</span><br><span class="line">		 * 查</span><br><span class="line">		 * */</span><br><span class="line">		</span><br><span class="line">		/*queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br><span class="line">		queryRunner.update(&quot;delete from account where id = ?&quot;, 8);</span><br><span class="line">		queryRunner.update(&quot;update account set money = ? where id = ?&quot;, 0, 10);*/</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>查询优化</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-b3e7cdea9f4befed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p>一个对象: BeanHandler<t></t></p>
<p>一个集合里面有很多对象: BeanListHandler<t></t></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.domain;</span><br><span class="line"></span><br><span class="line">public class Account &#123;</span><br><span class="line">	private String name;</span><br><span class="line">	private int money;</span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	public int getMoney() &#123;</span><br><span class="line">		return money;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setMoney(int money) &#123;</span><br><span class="line">		this.money = money;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Account [name=&quot; + name + &quot;, money=&quot; + money + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbutils;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.ResultSetHandler;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanHandler;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Account;</span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class TestDBUtils &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert() throws SQLException &#123;</span><br><span class="line">//		ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line"></span><br><span class="line">		// dbutils 简化了CRUD的代码</span><br><span class="line">		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">		// 查询的是一个Bean对象</span><br><span class="line">		// new 匿名实现类-&gt;new接口的匿名实现类</span><br><span class="line">		</span><br><span class="line">		/*Account account = queryRunner.query(&quot;select * from account where id = ?&quot;, new ResultSetHandler&lt;Account&gt;() &#123;</span><br><span class="line">			// 去执行查询,查询到的数据在result里面,然后调用handle方法,用户手动去封装</span><br><span class="line">			@Override</span><br><span class="line">			public Account handle(ResultSet rs) throws SQLException &#123;</span><br><span class="line">				// TODO Auto-generated method stub</span><br><span class="line">				Account account = new Account();</span><br><span class="line">				</span><br><span class="line">				while(rs.next()) &#123;</span><br><span class="line">					String name = rs.getString(&quot;name&quot;);</span><br><span class="line">					int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">					account.setName(name);</span><br><span class="line">					account.setMoney(money);</span><br><span class="line">				&#125;</span><br><span class="line">				return account;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;, 1);</span><br><span class="line">		System.out.println(account.toString());*/</span><br><span class="line">		</span><br><span class="line">		// 接口的实现类-&gt; ResultSetHandler的实现类</span><br><span class="line">		// Ctrl + t 实现类</span><br><span class="line">		//ResultSetHandler</span><br><span class="line">		</span><br><span class="line">		// 一个对象查询一个对象使用BeanHandler</span><br><span class="line">		Account account = queryRunner.query(&quot;select * from account where id = ?&quot;, new BeanHandler&lt;Account&gt;(Account.class), 1);</span><br><span class="line">		System.out.println(account.toString());</span><br><span class="line">		/*</span><br><span class="line">		 * queryRunner.update(sql);</span><br><span class="line">		 * 增删改</span><br><span class="line">		 * queryRunner.query(sql, rsh);</span><br><span class="line">		 * 查</span><br><span class="line">		 * */</span><br><span class="line">		</span><br><span class="line">		/*queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br><span class="line">		queryRunner.update(&quot;delete from account where id = ?&quot;, 8);</span><br><span class="line">		queryRunner.update(&quot;update account set money = ? where id = ?&quot;, 0, 10);*/</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>优化成两行代码:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-716550bdcbdd7c09.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="贴图"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Account account = queryRunner.query(&quot;select * from account where id = ?&quot;, new BeanHandler&lt;Account&gt;(Account.class), 1);</span><br><span class="line">System.out.println(account.toString());</span><br></pre></td></tr></table></figure>
<blockquote>
<p>查询多个数据</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-5d4dfbddb425954e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 查询多个对象</span><br><span class="line">List&lt;Account&gt; list = queryRunner.query(&quot;select * from account&quot;,</span><br><span class="line">				new BeanListHandler&lt;Account&gt;(Account.class));</span><br><span class="line">for(Account account : list) &#123;</span><br><span class="line"> System.out.println(account.toString());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dbutils;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.ResultSetHandler;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanHandler;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanListHandler;</span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Account;</span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class TestDBUtils &#123;</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert() throws SQLException &#123;</span><br><span class="line">//		ComboPooledDataSource dataSource = new ComboPooledDataSource(); </span><br><span class="line"></span><br><span class="line">		// dbutils 简化了CRUD的代码</span><br><span class="line">		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line">		// 查询的是一个Bean对象</span><br><span class="line">		// new 匿名实现类-&gt;new接口的匿名实现类</span><br><span class="line">		</span><br><span class="line">		/*Account account = queryRunner.query(&quot;select * from account where id = ?&quot;, new ResultSetHandler&lt;Account&gt;() &#123;</span><br><span class="line">			// 去执行查询,查询到的数据在result里面,然后调用handle方法,用户手动去封装</span><br><span class="line">			@Override</span><br><span class="line">			public Account handle(ResultSet rs) throws SQLException &#123;</span><br><span class="line">				// TODO Auto-generated method stub</span><br><span class="line">				Account account = new Account();</span><br><span class="line">				</span><br><span class="line">				while(rs.next()) &#123;</span><br><span class="line">					String name = rs.getString(&quot;name&quot;);</span><br><span class="line">					int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">					account.setName(name);</span><br><span class="line">					account.setMoney(money);</span><br><span class="line">				&#125;</span><br><span class="line">				return account;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;, 1);</span><br><span class="line">		System.out.println(account.toString());*/</span><br><span class="line">		</span><br><span class="line">		// 接口的实现类-&gt; ResultSetHandler的实现类</span><br><span class="line">		// Ctrl + t 实现类</span><br><span class="line">		//ResultSetHandler</span><br><span class="line">		</span><br><span class="line">		// 一个对象查询一个对象使用BeanHandler</span><br><span class="line">		/*Account account = queryRunner.query(&quot;select * from account where id = ?&quot;,</span><br><span class="line">				new BeanHandler&lt;Account&gt;(Account.class), 1);</span><br><span class="line">		System.out.println(account.toString());*/</span><br><span class="line">		</span><br><span class="line">		/*List&lt;Account&gt; account = queryRunner.query(&quot;select * from account&quot;,</span><br><span class="line">				new BeanListHandler&lt;Account&gt;(Account.class));*/</span><br><span class="line">		// 查询多个对象</span><br><span class="line">		List&lt;Account&gt; list = queryRunner.query(&quot;select * from account&quot;,</span><br><span class="line">				new BeanListHandler&lt;Account&gt;(Account.class));</span><br><span class="line">		for(Account account : list) &#123;</span><br><span class="line">			System.out.println(account.toString());</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		/*</span><br><span class="line">		 * queryRunner.update(sql);</span><br><span class="line">		 * 增删改</span><br><span class="line">		 * queryRunner.query(sql, rsh);</span><br><span class="line">		 * 查</span><br><span class="line">		 * */</span><br><span class="line">		</span><br><span class="line">		/*queryRunner.update(&quot;insert into account values(null, ?, ?)&quot;, &quot;bbb&quot;, 10000);</span><br><span class="line">		queryRunner.update(&quot;delete from account where id = ?&quot;, 8);</span><br><span class="line">		queryRunner.update(&quot;update account set money = ? where id = ?&quot;, 0, 10);*/</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="小结-1"><a href="#小结-1" class="headerlink" title="小结"></a>小结</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 通过类的字节码,获得该类的实例:</span><br><span class="line">Account a = new Account();</span><br><span class="line"></span><br><span class="line">Account a1 = Account.class.newInstance();</span><br><span class="line"></span><br><span class="line">new BeanHandler&lt;Account&gt;(Account.class)</span><br><span class="line"></span><br><span class="line">new BeanListHandler&lt;Account&gt;(Account.class)</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-38c6f62adadcc132.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="小结"></p>
<h2 id="ResultSetHandler-lt-T-gt"><a href="#ResultSetHandler-lt-T-gt" class="headerlink" title="ResultSetHandler&lt;T&gt;"></a><code>ResultSetHandler&lt;T&gt;</code></h2><p>实现类:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-eaeb4582c561c67b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="贴图"></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><blockquote>
<p>事务,连接池,DBUtils</p>
</blockquote>
<p>查询:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-3156e9e65b334c6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="贴图"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());</span><br><span class="line"></span><br><span class="line">queryRunner.update();</span><br><span class="line">queryRunner.query</span><br></pre></td></tr></table></figure>
<ol>
<li>事务<br>脏读<br>不可重复读<br>幻读</li>
</ol>
<p>丢失更新 写的问题<br>悲观锁 for update<br>乐观锁 ,添加字段版本级别</p>
<p>4个隔离级别<br>读未提交<br>读已提交<br>可重复读<br>可串行化</p>
<p>读未提交,引发脏读<br>读已提交,引发不可重复读,解决了脏读</p>
<p>可重复读,引发了幻读,解决了脏读,不可重复读<br>可串行化(序列化),解决了脏读,不可重复读,幻读,引发效率低下问题</p>
<p>数据库连接池</p>
<ol>
<li>BCP -&gt; 不使用配置 使用配置</li>
<li>C3P0 -&gt; 不使用配置 使用配置</li>
<li>自定义连接池 -&gt; 饰者模式</li>
</ol>
<h2 id="结言"><a href="#结言" class="headerlink" title="结言"></a>结言</h2><p>可在评论发表你的总结内容,做功课哦!</p>
<p>如果看了觉得不错</p>
<p>点赞！转发！</p>
<blockquote>
<p>达叔小生：往后余生，唯独有你<br>You and me, we are family !<br>90后帅气小伙，良好的开发习惯；独立思考的能力；主动并且善于沟通<br>简书博客： 达叔小生<br><a href="https://www.jianshu.com/u/c785ece603d1" target="_blank" rel="noopener">https://www.jianshu.com/u/c785ece603d1</a></p>
</blockquote>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><ul>
<li>下面我将继续对 其他知识 深入讲解 ，有兴趣可以继续关注</li>
<li>小礼物走一走 or 点赞</li>
</ul>
      
    </div>
    
    
    

      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
      </div>

    

      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/微信公众号.jpg" alt="达叔小生 wechat" style="width: 200px; max-width: 100%">
    <div>欢迎您扫一扫上面的微信公众号，订阅我的博客！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="达叔小生 微信支付">
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    达叔小生
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://huangguangda.github.io/2019/01/27/1/" title="第77节:Java中的事务和数据库连接池和DBUtils">https://huangguangda.github.io/2019/01/27/1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/带你学会Java基础课程/" rel="tag"><i class="fa fa-tag"></i> 带你学会Java基础课程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/20/1/" rel="next" title="公众号开发笔记一">
                <i class="fa fa-chevron-left"></i> 公众号开发笔记一
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/01/28/1/" rel="prev" title="第78节:Java中的网络编程(上)">
                第78节:Java中的网络编程(上) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNTU5OC8xMjEzNA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.gif" alt="达叔小生">
            
              <p class="site-author-name" itemprop="name">达叔小生</p>
              <p class="site-description motion-element" itemprop="description"></p>

            <div id="days"></div>
<script>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("04/23/1997 00:00:00");
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=setzero(Math.floor(e_hrsold));
e_minsold=(e_hrsold-hrsold)*60;
minsold=setzero(Math.floor((e_hrsold-hrsold)*60));
seconds=setzero(Math.floor((e_minsold-minsold)*60));
document.getElementById('days').innerHTML="已运行"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
function setzero(i){
if (i<10)
{i="0" + i};
return i;
}
show_date_time();
</script>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
                <a title="收藏到书签，偶尔High一下^_^" rel="alternate" class="mw-harlem_shake_slow wobble shake" href="javascript:(function() {
    function c() {
        var e = document.createElement(&quot;link&quot;);
        e.setAttribute(&quot;type&quot;, &quot;text/css&quot;);
        e.setAttribute(&quot;rel&quot;, &quot;stylesheet&quot;);
        e.setAttribute(&quot;href&quot;, f);
        e.setAttribute(&quot;class&quot;, l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement(&quot;div&quot;);
        e.setAttribute(&quot;class&quot;, a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement(&quot;audio&quot;);
        e.setAttribute(&quot;class&quot;, l);
        e.src = i;
        e.loop = false;
        e.addEventListener(&quot;canplay&quot;, function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener(&quot;ended&quot;, function() {
            N();
            h()
        }, true);
        e.innerHTML = &quot; <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>&quot;;
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + o
    }
 
    function T(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp(&quot;\\b&quot; + s + &quot;\\b&quot;);
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, &quot;&quot;)
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3&quot;;
    var s = &quot;mw-harlem_shake_me&quot;;
    var o = &quot;im_first&quot;;
    var u = [&quot;im_drunk&quot;, &quot;im_baked&quot;, &quot;im_trippin&quot;, &quot;im_blown&quot;];
    var a = &quot;mw-strobe_light&quot;;
    var f = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css&quot;;
    var l = &quot;mw_added_css&quot;;
    var b = g();
    var w = y();
    var C = document.getElementsByTagName(&quot;*&quot;);
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn(&quot;Could not find a node of the right size. Please try a different page.&quot;);
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()"><i class="fa fa-music"></i> High</a>

            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/huangguangda" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:2397923107@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/c785ece603d1" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-android"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36232611" target="_blank" title="csdn">
                      
                        <i class="fa fa-fw fa-spinner"></i>csdn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/186539024/" target="_blank" title="豆瓣">
                      
                        <i class="fa fa-fw fa-bath"></i>豆瓣</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/da-shu-xiao-sheng/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a16e1f3f265da43128096cb" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-google"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5507262458/profile?topnav=1&wvr=6" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI1NTcxOTQ1Nw==&scene=124#wechat_redirect" target="_blank" title="历史">
                      
                        <i class="fa fa-fw fa-apple"></i>历史</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://www.cnblogs.com/dashucoding/" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-facebook"></i>博客园</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/englishpages/index.html" target="_blank" title="英文简历">
                      
                        <i class="fa fa-fw fa-globe"></i>英文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/pages/" target="_blank" title="中文简历">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>中文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/GDone/events" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-twitter"></i>码云</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://my.oschina.net/xiaomaomi1997/" target="_blank" title="开源中国">
                      
                        <i class="fa fa-fw fa-facebook"></i>开源中国</a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa fa-fw fa-globe"></i>
                推荐阅读
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.alloyteam.com/nav/" title="Web前端导航" target="_blank">Web前端导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.36zhen.com/t?id=3448" title="前端书籍资料" target="_blank">前端书籍资料</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ife.baidu.com/" title="百度前端技术学院" target="_blank">百度前端技术学院</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://wf.uisdc.com/cn/" title="google前端开发基础" target="_blank">google前端开发基础</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.geekbang.org/" title="极客邦科技" target="_blank">极客邦科技</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ilxdh.com/" title="龙轩导航" target="_blank">龙轩导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://code.giffox.com/" title="程序员导航" target="_blank">程序员导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://search.chongbuluo.com/" title="快搜" target="_blank">快搜</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.android-doc.com/training/index.html" title="Android中文API" target="_blank">Android中文API</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://developer.android.google.cn/" title="Android Developers" target="_blank">Android Developers</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.freecodecamp.cn/" title="FreeCodeCamp中文社区" target="_blank">FreeCodeCamp中文社区</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://docs.oracle.com/javase/specs/" title="Java SE规范" target="_blank">Java SE规范</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://liuwangshu.cn/" title="刘望舒" target="_blank">刘望舒</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.colabug.com/author/3159ee71c93b3b4a/" title="小楼昨夜又秋风" target="_blank">小楼昨夜又秋风</a>
                  </li>
                
              </ul>

            </div>
          


        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事务"><span class="nav-number">2.</span> <span class="nav-text">事务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#回顾一下"><span class="nav-number">3.</span> <span class="nav-text">回顾一下</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#EL"><span class="nav-number">4.</span> <span class="nav-text">EL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSTL"><span class="nav-number">5.</span> <span class="nav-text">JSTL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库"><span class="nav-number">6.</span> <span class="nav-text">数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Repeatable-Read重复读"><span class="nav-number">7.</span> <span class="nav-text">Repeatable Read重复读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#可串化"><span class="nav-number">8.</span> <span class="nav-text">可串化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结"><span class="nav-number">9.</span> <span class="nav-text">小结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事务的安全隐患脏读"><span class="nav-number">10.</span> <span class="nav-text">事务的安全隐患脏读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Read-Committed读已提交"><span class="nav-number">11.</span> <span class="nav-text">Read Committed读已提交</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安全隐患-可重复读"><span class="nav-number">12.</span> <span class="nav-text">安全隐患_可重复读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置重复读"><span class="nav-number">13.</span> <span class="nav-text">设置重复读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安全隐患-可串行化"><span class="nav-number">14.</span> <span class="nav-text">安全隐患 _可串行化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事务-隔离级别小结"><span class="nav-number">15.</span> <span class="nav-text">事务_隔离级别小结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#写的问题-丢失更新"><span class="nav-number">16.</span> <span class="nav-text">写的问题_丢失更新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#写的问题-丢失更新-1"><span class="nav-number">17.</span> <span class="nav-text">写的问题_丢失更新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#丢失更新的问题"><span class="nav-number">18.</span> <span class="nav-text">丢失更新的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库连接池"><span class="nav-number">19.</span> <span class="nav-text">数据库连接池</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库连接池-C3P0"><span class="nav-number">20.</span> <span class="nav-text">数据库连接池_C3P0</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DBUtils"><span class="nav-number">21.</span> <span class="nav-text">DBUtils</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结-1"><span class="nav-number">22.</span> <span class="nav-text">小结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ResultSetHandler-lt-T-gt"><span class="nav-number">23.</span> <span class="nav-text">ResultSetHandler&lt;T&gt;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">24.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结言"><span class="nav-number">25.</span> <span class="nav-text">结言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">26.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="heart">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">达叔小生</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">357.6k</span>
  
</div>






  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共357.6k字</span>
</div>

<div class="weixin-box">
  <div class="weixin-menu">
    <div class="weixin-hover">
      <div class="weixin-description">微信扫一扫，订阅本博客</div>
    </div>
  </div>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客数</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>





  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=65809965";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  


  <script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script>
<script type="text/javascript" src="/js/src/custom.js"></script>


   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>


  <!-- 代码块复制功能 -->
  <script type="text/javascript" src="/js/src/clipboard.min.js"></script>  
  <script type="text/javascript" src="/js/src/clipboard-use.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"live2d-widget-model-hibiki"},"display":{"position":"right","width":75,"height":150},"mobile":{"show":false},"log":false});</script></body>
</html>